package edu.ccut.saturn.studio.example;

import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;

/**
 * 创建简单事例向导页——查询结果列表向导页，选择呈现在查询结果列表中的各个数据项字段名称并可调整字段的显示顺序。
 * 
 * @author 赵新峰
 * @author 施洪洁
 * @version 1.0 2009.12.02
 */

public class QueryListWiardPage extends SaturnWizardPage {

	private Button button_down;
	private Button button_up;
	private Button button_clear;
	private Button button_all;
	private Button button_delete;
	private Button button_add;
	public List list_end;
	private List list_start;
	private DataItemAtrributesVO dataItemAtrributesVO;

	public QueryListWiardPage(String pageName) {
		super(pageName);
	}

	public QueryListWiardPage(DataItemAtrributesVO dataItemAtrributesVO,
			String pagename) {
		super(pagename);
		setDescription(Messages.getString("QueryListWiardPage.queryList")); //$NON-NLS-1$
		this.dataItemAtrributesVO = dataItemAtrributesVO;
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		setControl(container);

		final Label sourceLabel = new Label(container, SWT.NONE);
		sourceLabel.setText(Messages
				.getString("QueryListWiardPage.sourceItems")); //$NON-NLS-1$
		new Label(container, SWT.NONE);

		final Label targetLabel = new Label(container, SWT.NONE);
		targetLabel.setText(Messages
				.getString("QueryListWiardPage.targetItems")); //$NON-NLS-1$

		list_start = new List(container, SWT.BORDER);
		list_start.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(final MouseEvent e) {
				addItemToEnd();
				valitate();
			}
		});
		list_start.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				String List_name = "List_start"; //$NON-NLS-1$
				button(List_name);
			}
		});
		final GridData gd_list_start = new GridData(226, 170);
		list_start.setLayoutData(gd_list_start);

		final Group group = new Group(container, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		group.setLayout(new GridLayout());

		button_add = new Button(group, SWT.NONE);
		button_add.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				addItemToEnd();
				valitate();
			}
		});
		button_add.setText(Messages.getString("QueryListWiardPage.add")); //$NON-NLS-1$

		button_delete = new Button(group, SWT.NONE);
		button_delete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				int selectedItemIndex = list_end.getSelectionIndex();
				String selectedItemName = list_end.getItem(selectedItemIndex);
				list_end.remove(selectedItemIndex);
				if (selectedItemIndex == 0) {
					list_end.select(0);
				}
				list_end.select(selectedItemIndex - 1);
				list_start.add(selectedItemName);
				if (list_end.getItemCount() == 0) {
					button_delete.setEnabled(false);
				}
				valitate();
			}
		});
		button_delete.setText(Messages.getString("QueryListWiardPage.delete")); //$NON-NLS-1$

		button_all = new Button(group, SWT.NONE);
		button_all.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				String[] currentItemInList_end = list_end.getItems();
				int lengthOfCurrentItemInList_end = currentItemInList_end.length;
				String[] currentItemInList_start = list_start.getItems();
				int lengthOfCurrentItemInList_start = currentItemInList_start.length;
				int lengthOfFinalItemInList_end = lengthOfCurrentItemInList_end
						+ lengthOfCurrentItemInList_start;
				String[] finalItemInList_end = new String[lengthOfFinalItemInList_end];
				System.arraycopy(currentItemInList_end, 0, finalItemInList_end,
						0, lengthOfCurrentItemInList_end);
				System.arraycopy(currentItemInList_start, 0,
						finalItemInList_end, lengthOfCurrentItemInList_end,
						lengthOfCurrentItemInList_start);
				list_end.setItems(finalItemInList_end);
				list_start.removeAll();
				list_end.setSelection(0);
				button_delete.setEnabled(true);
				button_up.setEnabled(false);
				button_down.setEnabled(true);
				valitate();
			}
		});
		button_all.setText(Messages.getString("QueryListWiardPage.all")); //$NON-NLS-1$

		button_clear = new Button(group, SWT.NONE);
		button_clear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				String[] itemsInEnd = list_end.getItems();
				int sizeOfItemsInEnd = itemsInEnd.length;
				int sizeOfStart = list_start.getItemCount();
				for (int i = 0; i < sizeOfItemsInEnd; i++) {
					list_start.add(itemsInEnd[i], i + sizeOfStart);
				}
				list_end.removeAll();
				list_start.setSelection(0);
				button_delete.setEnabled(false);
				button_up.setEnabled(false);
				button_down.setEnabled(false);
				valitate();
			}
		});
		button_clear.setText(Messages.getString("QueryListWiardPage.clear")); //$NON-NLS-1$

		button_up = new Button(group, SWT.NONE);
		button_up.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				int selectedDataItemIndex = list_end.getSelectionIndex();
				if (selectedDataItemIndex != 0) {
					String selectedDataItemName = list_end
							.getItem(selectedDataItemIndex);
					list_end.remove(selectedDataItemIndex);
					String previousSelectedDataItemName = list_end
							.getItem(selectedDataItemIndex - 1);
					list_end.remove(selectedDataItemIndex - 1);
					list_end.add(selectedDataItemName,
							selectedDataItemIndex - 1);
					list_end.add(previousSelectedDataItemName,
							selectedDataItemIndex);
					list_end.select(selectedDataItemIndex - 1);
					if (selectedDataItemIndex - 1 == 0) {
						button_up.setEnabled(false);
					} else {
						button_down.setEnabled(true);
					}
				}
			}
		});
		button_up.setText(Messages.getString("QueryListWiardPage.up")); //$NON-NLS-1$

		button_down = new Button(group, SWT.NONE);
		button_down.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				int selectedDataItemIndex = list_end.getSelectionIndex();
				if (selectedDataItemIndex != list_end.getItemCount()) {
					String selectedDataItemName = list_end
							.getItem(selectedDataItemIndex);
					String nextSelectedDataItemName = list_end
							.getItem(selectedDataItemIndex + 1);
					list_end.remove(selectedDataItemIndex + 1);
					list_end.remove(selectedDataItemIndex);
					list_end.add(nextSelectedDataItemName,
							selectedDataItemIndex);
					list_end.add(selectedDataItemName,
							selectedDataItemIndex + 1);
					list_end.select(selectedDataItemIndex + 1);
					if (selectedDataItemIndex + 1 == list_end.getItemCount() - 1) {
						button_down.setEnabled(false);
						button_up.setEnabled(true);
					} else {
						button_up.setEnabled(true);
					}
				}
			}
		});
		button_down.setText(Messages.getString("QueryListWiardPage.down")); //$NON-NLS-1$

		list_end = new List(container, SWT.BORDER);
		list_end.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				String List_name = "List_end"; //$NON-NLS-1$
				button(List_name);
			}
		});
		final GridData gd_list_end = new GridData(226, 170);
		list_end.setLayoutData(gd_list_end);

		if (dataItemAtrributesVO.getDataItems() != null) {
			int dataItemNumber = dataItemAtrributesVO.getDataItems().size();
			String[] dataItem = new String[dataItemNumber];
			for (int i = 0; i < dataItemNumber; i++) {
				dataItem[i] = dataItemAtrributesVO.getDataItems().get(i)
						.getName();
			}
			list_start.setItems(dataItem);
			list_start.setSelection(0);
			button_delete.setEnabled(false);
			button_up.setEnabled(false);
			button_down.setEnabled(false);

		}

		valitate();
	}

	public boolean contains(String[] stringArray, String str) {
		for (int i = 0; i < stringArray.length; i++) {
			if (str.equals(stringArray[i])) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void initalValues(Map<String, Object> values) {

	}

	/**
	 * 控制按钮是否可用的方法
	 * 
	 * @param listName
	 * @return
	 * 
	 */
	public void button(String listName) {
		if (listName.equals("List_start")) { //$NON-NLS-1$
		} else {
			if (list_end.getItemCount() != 0) {
				this.button_delete.setEnabled(true);
			}
			if (list_end.getItemCount() != 1
					&& list_end.getSelectionIndex() != 0) {
				this.button_up.setEnabled(true);
			} else {
				this.button_up.setEnabled(false);

			}
			if (list_end.getItemCount() != 1
					&& list_end.getSelectionIndex() != list_end.getItemCount() - 1) {
				this.button_down.setEnabled(true);
			} else {
				this.button_down.setEnabled(false);
			}
		}
	}

	public void addItemToEnd() {
		int indexOfSelectedDataItem = list_start.getSelectionIndex();
		int currentSizeOfList_start = list_start.getItemCount();
		String selectedDataItemName = list_start
				.getItem(indexOfSelectedDataItem);
		list_end.add(selectedDataItemName);
		list_start.remove(list_start.getSelectionIndex());
		if (indexOfSelectedDataItem != currentSizeOfList_start - 1) {
			list_start.select(indexOfSelectedDataItem);
		} else {
			list_start.select(indexOfSelectedDataItem - 1);
		}
		list_end.deselectAll();
		button_delete.setEnabled(false);
		button_up.setEnabled(false);
		button_down.setEnabled(false);
		if (list_start.getItemCount() == 0) {
			list_end.select(list_end.getItemCount() - 1);
			button_delete.setEnabled(true);
			button_up.setEnabled(true);
		}
	}

	/**
	 * 判断list_end中的列表项是否为空
	 * 
	 */
	public boolean valitate() {
		setPageComplete(false);

		if (list_end.getItemCount() != 0) {
			setPageComplete(true);
			return true;
		}
		return false;
	}
}
